package com.bluesky;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bluesky.dreamer.entity.LoginMessage;

/**
 * Servlet Filter implementation class SessionFilter
 */
public class SessionFilter implements Filter {
	Log logger = LogFactory.getLog(SessionFilter.class);

	/**
	 * Default constructor.
	 */
	public SessionFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		boolean flag = false ;
		HttpServletRequest hRequest = (HttpServletRequest) request;
		HttpServletResponse hResponse = (HttpServletResponse) response;
		String requestURI = hRequest.getRequestURI();
		String contextPath = hRequest.getContextPath();
		HttpSession session = hRequest.getSession();
		String realpath=hRequest.getScheme()+"://"+hRequest.getServerName()+":"+hRequest.getServerPort()+contextPath;
		LoginMessage uSession = (LoginMessage) session.getAttribute("loginsession");
		if (uSession!= null) {
			chain.doFilter(request, response);
		} else {     
			chain.doFilter(request, response);
			/*if(requestURI.endsWith("login!login.do")||requestURI.endsWith("admin/main.jsp")
			   ||requestURI.endsWith("sessionTimeout.jsp")||requestURI.contains("njs/admin/")
			   ||requestURI.endsWith("admin/login.jsp")||requestURI.endsWith("bluesky/login")){
				chain.doFilter(request, response);
			}else{
				logger.info("session time out:haven't uSesion,go to login page");
				String lout=contextPath+"/admin/sessionTimeout.jsp";
				hResponse.sendRedirect(lout);
				chain.doFilter(request, response);
		  }*/
		}
	}

	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
